package com.hqyj.ssm_project.controller;

import com.hqyj.ssm_project.pojo.Detp;
import com.hqyj.ssm_project.service.DetpService;
import com.hqyj.ssm_project.service.UserService;
import com.hqyj.ssm_project.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

@Controller
public class DetpController {
    @Autowired
    private DetpService detpService;
    @Autowired
    private UserService userService;

    @RequestMapping(value = "/deptPage.do")
    public String deptPage(Integer currentNum,Model model) {
        if(currentNum==null){
            currentNum=1;
        }
//        List<Detp> detpList = detpService.selectDetp();
        PageUtil<Detp> detpPageUtil=detpService.selectDetpLimit(currentNum);
        model.addAttribute("detpList", detpPageUtil);
        return "deptPage";
    }
    @RequestMapping(value = "/addDeptPage.do")
    public String addDeptPage() {
        return "addDeptPage";
    }
    @RequestMapping(value = "/addDept.do")
    public String addDept(Detp detp) {
        detpService.insertDetp(detp);
        return "redirect:deptPage.do";
    }
    @RequestMapping(value = "/updateDeptPage.do")
    public String updateDeptPage(Integer id, Model model) {
        Detp detp = detpService.selectByPrimaryKey(id);
        model.addAttribute("detp", detp);
        return "updateDeptPage";
    }
    @RequestMapping(value = "/updateDept.do", method = RequestMethod.POST)
    public String updateDept(Detp detp) {
        detpService.updateByPrimaryKey(detp);
        return "redirect:deptPage.do";
    }
    @RequestMapping(value = "/deleteDept.do")
    public String deleteDept(Integer id){
        userService.updateUserByDeptId(id);
        detpService.deleteByPrimaryKey(id);
        return "redirect:deptPage.do";
    }
}
